package com.zlj.accountant.app.weiget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.zlj.accountant.R;

public class ListItem extends View{
	
	View root;
	
	ViewGroup vTop,vBottom;
	
	Button btnOpen;
	
	public interface ListItemButton{

		public CharSequence getTitle();

		public int getImg();

		public OnClickListener getOnClickListener();
		
	}

	public ListItem(Context context,ViewGroup parent){
	    super(context);
	    LayoutInflater factory = LayoutInflater.from(context);
	    root = factory.inflate(R.layout.lyt_widget_listitem, null);
	    parent.addView(root);
	    initWidget();
	}
	
	private void initWidget(){
	    vTop = (ViewGroup)root.findViewById(R.id.v_top);
	    vBottom = (ViewGroup)root.findViewById(R.id.v_bottom);
	    btnOpen = (Button)root.findViewById(R.id.btn_open);
	    btnOpen.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v){
				vBottom.setVisibility(vBottom.getVisibility() == VISIBLE ? GONE : VISIBLE);
			}
		});
    }

	public void setView(View view){
		vTop.removeAllViews();
		vTop.addView(view);
	}
	
	public void addButton(final ListItemButton lib){
		LayoutInflater factory = LayoutInflater.from(getContext());
		ViewGroup lin = (ViewGroup)factory.inflate(R.layout.lyt_widget_listbutton, null);
		vBottom.addView(lin);	
		LayoutParams p = (LayoutParams)lin.getLayoutParams();
		p.weight = 1;
		lin.setLayoutParams(p);
		TextView tv = (TextView)lin.findViewById(R.id.txt_label);
		ImageView iv = (ImageView)lin.findViewById(R.id.v_img);
		tv.setText(lib.getTitle());
		iv.setBackgroundResource(lib.getImg());
		lin.setOnClickListener(lib.getOnClickListener());
	}

}
